/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.page;

import com.cleangwt.page.client.PageContext.CodeSplit;
import com.cleangwt.page.client.PageContext.Page;
import com.cleangwt.page.client.PageContext.Prefetch;
import com.cleangwt.showcase.client.component.ShowcasePage;
import com.cleangwt.showcase.client.resource.PageGroups;
import com.cleangwt.user.client.ui.Lay;
import com.cleangwt.user.client.ui.TablePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;

/**
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
@Prefetch(PageGroups.BASE_WIDGET)
@CodeSplit
@Page
public class LayoutDemo extends ShowcasePage {

  public LayoutDemo() {
    super(
        "TablePanel & Lay",
        "The <code>TablePanel</code> for laying widget in table-tr-td style, and The <code>Lay</code> is the layout helper.",
        "LayoutDemo.java");
    TablePanel layout = new TablePanel(true);
    setContentWidget(Lay.hStretch(layout));

    layout.addRow(getPromptAndButtons(6));
    // row span3
    layout.addRow(Lay.rowSpan(3, "ColSpan"), Lay.colSpan(5, Lay.hor(getBoxes(3))));
    layout.addRow(Lay.colSpan(5, Lay.center(getBoxes(3))));
    layout.addRow(Lay.colSpan(5, Lay.right(getBoxes(3))));
    // end row span3

    // row span6
    layout.addRow(Lay.rowSpan(6, "RowSpan"), Lay.rowSpan(2, Lay.top(getBoxes(3))), Lay
        .ver(getBoxes(4)));
    layout.addRow("Empty Row");
    layout.addRow(Lay.rowSpan(2, Lay.middle(getBoxes(3))), Lay.ver(getBoxes(4)));
    layout.addRow("Empty Row");
    layout.addRow(Lay.rowSpan(2, Lay.bottom(getBoxes(3))), Lay.ver(getBoxes(4)));
    layout.addRow("Empty Row");
    // end row span6
  }

  private Object[] getBoxes(int count) {
    Object widgets[] = new Object[count];
    for (int i = 0; i < count; i++) {
      Label lbl = new Label();
      lbl.setSize("20px", "20px");
      lbl.getElement().getStyle().setProperty("border", "1px solid #333333");
      widgets[i] = lbl;
    }
    return widgets;
  }

  private Object[] getPromptAndButtons(int count) {
    Object widgets[] = new Object[count];
    for (int i = 0; i < count; i++) {
      widgets[i] = i % 2 == 1 ? new Button("btn") : "Prompt";
    }
    return widgets;
  }

}
